<?php

class Users_model extends Model
{
        function  __construct()
        {
            parent::Model();
        }

        function insert_user($firstname, $surname, $password, $user_level)
        {
            $data = array(
                'firstname'  => $firstname,
                'surname'    => $surname,
                'password'   => $password,
                'user_level' => $user_level
            );

            $this->db->insert('users', $data);

            return $this->db->insert_id();
        }
	
	function validate_user($firstname, $surname, $password, $return_id = TRUE) {
		$query = $this->db->query("
			SELECT user_id
			FROM users
			WHERE firstname = " . $this->db->escape($firstname) . "
			AND surname = " . $this->db->escape($surname) . "
			AND password = " . $this->db->escape(md5($password)) . "
		");
		
		if ($query->num_rows() == 1) {
			if ($return_id) {
				$row = $query->row_array();
				return $row['user_id'];
			}
		}
		
		return ($query->num_rows() == 1);
	}
	
	function get_userdata($id) {
		$query = $this->db->query("
			SELECT user_id, firstname, surname, user_level
			FROM users
			WHERE user_id = " . $this->db->escape($id) . "
		");
		
		if ($query->num_rows() == 1) {
			return $query->row_array();
		}
		
		return FALSE;
	}

        function fullname_exists($firstname, $surname)
        {
            $this->db->select('*');
            $this->db->from('users');
            $this->db->where('firstname', $firstname);
            $this->db->where('surname', $surname);

            $query = $this->db->get();

            return ($query->num_rows() > 0);
        }

        function all_users()
        {
            $query = $this->db->get('users');
            return $query->result_array();
        }
	
}

?>